package com.hsc.account;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import com.hsc.db.tools.AccountList;
import com.hsc.db.tools.AccountService;
import com.hsc.db.tools.EmailService;
import com.hsc.db.tools.ItemList;
import com.hsc.db.tools.ItemService;
import com.hsc.db.tools.UseItemService;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class AddAmt extends Dialog {

	public AddAmt(Context context) {
		super(context);
	}

	public AddAmt(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private View contentView;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private  EditText  txt;
		private  EditText  txtAmt;
//        private int pagenum=10;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;

			return this;
		}

		public Builder setPositiveButton(String positiveButtonText,
										 DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,
										 DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		public AddAmt create(final String use,final TextView tzhichu,final GridView gview ,final ListView lview,final Context context,final AccountService accs,final int year,final int month,final int day,final ItemService items,final UseItemService typeService) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final AddAmt dialog = new AddAmt(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.addamt, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			txt = (EditText)layout.findViewById(R.id.remark);
			txtAmt = (EditText)layout.findViewById(R.id.amt);

			((EditText) layout.findViewById(R.id.remark)).setOnFocusChangeListener(new View.OnFocusChangeListener() {

				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if(hasFocus)
					{
						txtAmt.setCursorVisible(false);
					}else
					{
						txtAmt.setCursorVisible(true);
					}

				}
			});


			((EditText) layout.findViewById(R.id.amt)).setOnKeyListener(new View.OnKeyListener() {

				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					if(keyCode == KeyEvent.KEYCODE_ENTER){
						if(txt.getText().toString().length()==0)
						{
							Toast.makeText(context, "请输入备注", Toast.LENGTH_SHORT).show();
							txt.requestFocus();
						}
						else if(txtAmt.getText().toString().length()==0)
						{
							Toast.makeText(context, "请输入金额", Toast.LENGTH_SHORT).show();
							txtAmt.requestFocus();
						}else
						{


							AccountList acc = new AccountList();

							acc.setType(txt.getText().toString());
							acc.setAmt(Double.parseDouble(txtAmt.getText().toString()));
							acc.setBalance(1);
							acc.setDate(String.format("%04d", year)+"-"+String.format("%02d", month)+"-"+String.format("%02d",day));

							Time time = new Time();
							time.setToNow();

							String times = String.valueOf(time.hour)+":"+String.valueOf(time.minute)+":"+String.valueOf(time.second);
							acc.setTime(times);
							String uuid="";
							if(items.getlDataFlag("1")!=null)
							{
								uuid  =  items.getlDataFlag("1").getType()+UUID.randomUUID().toString();

							}else
							{
								uuid  =  UUID.randomUUID().toString();
							}

							acc.setRemark(uuid);
							acc.setUse(use);


							accs.save(acc);

							txt.setText("");
							txtAmt.setText("");

							txt.requestFocus();


							acc.setId(1);

							ArrayList<AccountList> arr = new ArrayList<AccountList>();
							arr.add(acc);


							sendEmailTread tread = new sendEmailTread(arr, items,accs);
							tread.start();


							//加载数据
							List<Map<String, Object>> data = typeService.getData(year, month);
							gridadapter2 sim_adapter = new gridadapter2(context, data,use);
							gview.setAdapter(sim_adapter);

							List<AccountList> cur1 = accs.getListDataByType(year,month,use,twoActivity.pagenum,0);
							ListViewAdapter adapter = new ListViewAdapter(context,cur1,items);
							lview.setAdapter(adapter);

							tzhichu.setText(String.format("%.1f",accs.getZSum("1",year,month)));

							txt.requestFocus();

							Toast.makeText(context, "添加成功！继续添加", Toast.LENGTH_SHORT).show();


						}

					}
					return false;
				}
			});



			txt.requestFocus();

			openInputMethod(txt);

			((Button) layout.findViewById(R.id.btnQuxiao))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							dialog.dismiss();

						}
					});




			((Button) layout.findViewById(R.id.btnQueren))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {

							if(txt.getText().toString().length()==0)
							{
								Toast.makeText(context, "请输入备注", Toast.LENGTH_SHORT).show();
								txt.requestFocus();
							}
							else if(txtAmt.getText().toString().length()==0)
							{
								Toast.makeText(context, "请输入金额", Toast.LENGTH_SHORT).show();
								txtAmt.requestFocus();
							}else
							{


								AccountList acc = new AccountList();

								acc.setType(txt.getText().toString());
								acc.setAmt(Double.parseDouble(txtAmt.getText().toString()));
								acc.setBalance(1);
								acc.setDate(String.format("%04d", year)+"-"+String.format("%02d", month)+"-"+String.format("%02d",day));

								Time time = new Time();
								time.setToNow();

								String times = String.valueOf(time.hour)+":"+String.valueOf(time.minute)+":"+String.valueOf(time.second);
								acc.setTime(times);

								String uuid="";
								if(items.getlDataFlag("1")!=null)
								{
									uuid  =  items.getlDataFlag("1").getType()+UUID.randomUUID().toString();

								}else
								{
									uuid  =  UUID.randomUUID().toString();
								}
								acc.setRemark(uuid);
								acc.setUse(use);

								accs.save(acc);

								acc.setId(1);

								ArrayList<AccountList> arr = new ArrayList<AccountList>();
								arr.add(acc);

								sendEmailTread tread = new sendEmailTread(arr, items,accs);
								tread.start();

								//加载数据
								List<Map<String, Object>> data = typeService.getData(year, month);
								gridadapter2 sim_adapter = new gridadapter2(context, data,use);
								gview.setAdapter(sim_adapter);

								List<AccountList> cur1 = accs.getListDataByType(year,month,use,twoActivity.pagenum,0);
								ListViewAdapter adapter = new ListViewAdapter(context,cur1,items);
								lview.setAdapter(adapter);

								tzhichu.setText(String.format("%.1f",accs.getZSum("1",year,month)));

								txt.requestFocus();


								InputMethodManager imm = (InputMethodManager) Builder.this.context.getSystemService(Context.INPUT_METHOD_SERVICE);
								imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

								dialog.dismiss();
							}


						}
					});



			dialog.setContentView(layout);


			return dialog;
		}

		public void openInputMethod(final EditText editText) {

			Timer timer = new Timer();

			timer.schedule(new TimerTask() {

				public void run() {

					InputMethodManager inputManager = (InputMethodManager) editText

							.getContext().getSystemService(

									Context.INPUT_METHOD_SERVICE);

					inputManager.showSoftInput(editText, 0);

				}

			}, 200);
		}

	}
}